*****************************************************************************
* LACIR_plots.do
* Brian Kovak - Apr 30, 2022
*
* Simplified code to produce Bellon-style plots from cleaned data in 
* inequality-outcomes-2020.dta
*****************************************************************************

set more off
clear all

*******************************************************
* Setup

use "inequality-outcomes-2020.dta", clear

**** Check for unique identifiers ****
isid country year

**** Create dummy variables representing the difference between the year and the year of liberalization ****

gen tau = year - libyear
tab tau, gen(tau_fe)

**** Code country variable ****
egen ctry_code = group(country)
tab ctry_code
save LACIR_plots, replace

*******************************************************
* Openness

use LACIR_plots, clear

**** Change in openness per year vs. average openness twenty years before liberalization ****
sort country year
by country: egen openness_pre_temp = mean(openness) if inrange(tau,-20,0)
by country: egen openness_pre = median(openness_pre_temp)
drop openness_pre_temp
gen openness_chg = openness - openness_pre

**** Year with maximum openness & minimum openness ****
tab country if openness_chg < .
by country: egen openness_year_min = min(year) if openness < .
by country: egen openness_year_max = max(year) if openness < .
preserve

**** Table 1 - Openness ****
collapse (median) openness_year_min openness_year_max, by(country)
list country openness_year_min openness_year_max, clean
save openness, replace
restore

*** Regression ****
reg openness_chg tau_fe17-tau_fe57 if inrange(tau,-20,20) [aw=population], nocons robust

matrix Beta = (e(b))'
matrix list Beta
matrix VC = e(V)
matrix SE = (vecdiag(cholesky(diag(vecdiag(VC)))))'
matrix list Beta
matrix list SE

**** Generate beta, SE, CI, Moving-average ****
svmat Beta
svmat SE
keep Beta1 SE1

rename Beta1 beta
rename SE1 se
gen ci_upper = beta + 1.96 * se
gen ci_lower = beta - 1.96 * se
gen tau = _n-21
tsset tau
gen ma = (1/3)*(beta+l.beta+f.beta) if inrange(tau,-20+1,20-1)
drop if _n > 41

twoway (scatter beta tau if inrange(tau,-20,20), msymb(Oh) mcolor(black) msize(small)) ///
       (line ma tau if inrange(tau,-20,20), lcolor(black) lwidth(medthick)) ///
       (line ci_upper tau if inrange(tau,-20,20), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   (line ci_lower tau if inrange(tau,-20,20), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   , legend(off) xtitle("years since liberalization") ///
	     ytitle("chg. in trade/GDP x 100 (vs. pre-lib average)") ///
		 graphregion(color(white)) xline(0, lcolor(gs6)) ///
		 note("Pre-liberalization average calculated over periods -20 to 0 relative to liberaliztaion.")
graph export "./output/openness_wgt.pdf", replace

*******************************************************
* Gini index (Inequality)

use LACIR_plots, clear

**** Change in gini index per year vs. average gini index 10 years before liberalization ****
sort country year
by country: egen gini_pre_temp = mean(gini_hat) if inrange(tau,-10,0)
by country: egen gini_pre = median(gini_pre_temp)
drop gini_pre_temp
gen gini_chg = gini_hat - gini_pre

**** Year with maximum openness & minimum gini index ****
tab country if gini_chg < .
by country: egen gini_year_min = min(year) if gini_hat < .
by country: egen gini_year_max = max(year) if gini_hat < .
preserve

**** Table 1 - Inequality ****
collapse (median) gini_year_min gini_year_max, by(country)
list country gini_year_min gini_year_max, clean
save inequality, replace
restore

*** Regression ****
reg gini_chg tau_fe27-tau_fe57 if inrange(tau,-10,20) [aw=population], nocons robust

matrix Beta = (e(b))'
matrix list Beta
matrix VC = e(V)
matrix SE = (vecdiag(cholesky(diag(vecdiag(VC)))))'
matrix list Beta
matrix list SE

svmat Beta
svmat SE
keep Beta1 SE1

**** Generate beta, SE, CI, Moving-average ****
rename Beta1 beta
rename SE1 se
gen ci_upper = beta + 1.96 * se
gen ci_lower = beta - 1.96 * se
gen tau = _n-11
tsset tau
gen ma = (1/3)*(beta+l.beta+f.beta) if inrange(tau,-10+1,20-1)
drop if _n > 31

twoway (scatter beta tau if inrange(tau,-10,20), msymb(Oh) mcolor(black) msize(small)) ///
       (line ma tau if inrange(tau,-10,20), lcolor(black) lwidth(medthick)) ///
       (line ci_upper tau if inrange(tau,-10,20), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   (line ci_lower tau if inrange(tau,-10,20), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   , legend(off) xtitle("years since liberalization") ///
	     ylabel(-6(2)6) ///
	     ytitle("chg. in Gini index x 100 (vs. pre-lib average)") ///
		 graphregion(color(white)) xline(0, lcolor(gs6)) ///
		 note("Pre-liberalization average calculated over periods -10 to 0 relative to liberaliztaion.")
graph export "./output/gini_wgt.pdf", replace


*******************************************************
* Unemployment

use LACIR_plots, clear

**** Change in unemployment rate per year vs. average unemployment rate 5 years before liberalization ****
sort country year
by country: egen unemployment_pre_temp = mean(unemployment) if inrange(tau,-5,0)
by country: egen unemployment_pre = median(unemployment_pre_temp)
drop unemployment_pre_temp
gen unemployment_chg = unemployment - unemployment_pre
sum unemployment_chg

**** Year with maximum openness & minimum unemployment rate ****
tab country if unemployment_chg < .
by country: egen unemployment_year_min = min(year) if unemployment < .
by country: egen unemployment_year_max = max(year) if unemployment < .
preserve

**** Table 1 - Unemployment ****
collapse (median) unemployment_year_min unemployment_year_max, by(country)
list country unemployment_year_min unemployment_year_max, clean
save unemployment, replace
restore

*** Regression ****
reg unemployment_chg tau_fe32-tau_fe57 if inrange(tau,-5,20) [aw=population], nocons robust

matrix Beta = (e(b))'
matrix VC = e(V)
matrix SE = (vecdiag(cholesky(diag(vecdiag(VC)))))'
matrix list Beta
matrix list SE

svmat Beta
svmat SE
keep Beta1 SE1

**** Generate beta, SE, CI, Moving-average ****
rename Beta1 beta
rename SE1 se
gen ci_upper = beta + 1.96 * se
gen ci_lower = beta - 1.96 * se
gen tau = _n-6
tsset tau
gen ma = (1/3)*(beta+l.beta+f.beta) if inrange(tau,-5+1,20-1)
drop if _n > 26

twoway (scatter beta tau if inrange(tau,-5,20), msymb(Oh) mcolor(black) msize(small)) ///
       (line ma tau if inrange(tau,-5,20), lcolor(black) lwidth(medthick)) ///
       (line ci_upper tau if inrange(tau,-5,20), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   (line ci_lower tau if inrange(tau,-5,20), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   , legend(off) xtitle("years since liberalization") ///
	     ytitle("chg. in unemployment rate x 100 (vs. pre-lib average)") ///
		 graphregion(color(white)) xline(0, lcolor(gs6)) ///
		 note("Pre-liberalization average calculated over periods -5 to 0 relative to liberaliztaion.")
graph export "./output/unemployment_wgt.pdf", replace


*******************************************************
* Informality

use LACIR_plots, clear

**** Change in informality rate per year vs. average informality rate 5 years before liberalization ****
sort country year
by country: egen informality_pre_temp = mean(informality) if inrange(tau,-5,0)
by country: egen informality_pre = median(informality_pre_temp)
drop informality_pre_temp
gen informality_chg = informality - informality_pre
sum informality_chg

**** Year with maximum openness & minimum informality rate ****
tab country if informality_chg < .
by country: egen informality_year_min = min(year) if informality < .
by country: egen informality_year_max = max(year) if informality < .
preserve

**** Table 1 - Informality ****
collapse (median) informality_year_min informality_year_max, by(country)
list country informality_year_min informality_year_max, clean
save informality, replace
restore

*** Regression ****
reg informality_chg tau_fe32-tau_fe34 tau_fe36-tau_fe57 if inrange(tau,-5,20) [aw=population], nocons robust

matrix Beta= (e(b))'
matrix VCtmp = e(V)
matrix Var = (vecdiag(VCtmp))'
local v = rowsof(Var)
matrix SE = J(rowsof(Var),1,0)
forvalue i=1/`v' {
	matrix SE[`i',1]=sqrt(Var[`i',1])
}
matrix list SE

svmat Beta
svmat SE

**** Generate beta, SE, CI, Moving-average ****
keep Beta1 SE1
rename Beta1 beta
rename SE1 se
gen tau = _n-5
replace tau = -5 if _n==1
replace tau = -4 if _n==2
replace tau = -3 if _n==3
replace tau = -2 if _n==26
sort tau
drop if _n >= 27

gen ci_upper = beta + 1.96 * se
gen ci_lower = beta - 1.96 * se
tsset tau
gen ma = (1/3)*(beta+l.beta+f.beta) if inrange(tau,-5+1,20-1)
replace ma = beta if inlist(tau,-1,1)

* omits periods with single observations and therefore single-point error bands
twoway (scatter beta tau if inrange(tau,-5,20) & !inlist(tau,-5,-3,0), msymb(Oh) mcolor(black) msize(small)) ///
       (line ma tau if inrange(tau,-5,20) & !inlist(tau,-5,-3,0), lcolor(black) lwidth(medthick)) ///
       (line ci_upper tau if inrange(tau,-5,20) & !inlist(tau,-5,-3,0), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   (line ci_lower tau if inrange(tau,-5,20) & !inlist(tau,-5,-3,0), lwidth(vthin) lpattern(-) lcolor(black)) ///
	   , legend(off) xtitle("years since liberalization") ///
	     ytitle("chg. in informality rate x 100 (vs. pre-lib average)") ///
		 graphregion(color(white)) xline(0, lcolor(gs6)) ///
		 note("Pre-liberalization average calculated over periods -5 to 0 relative to liberaliztaion.")
graph export "./output/informality_wgt.pdf", replace


**************************************************************
* Table 1

use openness, clear
merge 1:1 countrycode using "inequality.dta", nogen
merge 1:1 countrycode using "unemployment.dta", nogen
merge 1:1 countrycode using "informality.dta", nogen
merge 1:1 countrycode using "libyears.dta", keepusing(libyear)
keep if _merge == 3
drop _merge

export delimited using "./output/table1.csv", replace

**************************************************************
* Table A2

use "inequality-outcomes-2020.dta", clear
keep if libyear < . // keep LAC countries
keep if gini_hat < . // keep country-years with Gini measure
keep if year <= libyear // keep years up to liberalization year
by countrycode: egen max_pre_year = max(year) // latest pre-liberalization year with Gini measure
keep if year == max_pre_year

replace countrycode = "ELS" if countrycode == "SLV"
sort countrycode
keep countrycode year gini_hat

export delimited using "./output/tablea2.csv", replace



	     
	   
